<?php
/**
 * Zend Framework application
 *
 * @category     Application
 * @package      Admin
 * @subpackage   Validate
 * @author       Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright    Copyright (c) 2010 Krzysztof Kardasz
 */

/**
 * Walidacja, widoczność węzła
 * 
 * @category     Application
 * @package      Admin
 * @subpackage   Validate
 * @author       Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright    Copyright (c) 2010 Krzysztof Kardasz
 * @version 	 1.0
 */
class Hesperos_Validator_NodeVisible extends Zend_Validate_Abstract
{
    /**
     * Wiadomości błędów
     */
    const MSG_INVALID = 'msgInvalid';
 
    /**
     * Wiadomości błędów
     * 
     * @var array
     */
    protected $_messageTemplates = array(
        self::MSG_INVALID => 'Node cannot be visible, becouse parent is not visible'
    );
    
    /**
     * Walidacja 
     * 
     * @param string $value wartość
     * @return boolean
     */
    public function isValid ($value, $context = null)
    {
        $this->_setValue($value);
        
        $parent_id = 0;
        $language  = null;
        if (is_array($context)) {
            $parent_id = (isset($context['frm_parent_id'])) ? $context['frm_parent_id'] : 0;
            $language  = (isset($context['frm_le'])) ? $context['frm_le'] : null;
        }
                
        if($parent_id == 0 || $value == 0) {
            return true;
        }
        
        $table = new Application_Model_Gateway_Table_View_Node ();
        
        $select = $table->select()->from($table->getTableName(), array('is_visible'));
        $select->where('id = ?', $parent_id);
        $select->where('language = ?', $language);
        

        if ($select->query()->fetchColumn() != $value) {
            $this->_error(self::MSG_INVALID);
            return false;
        }
        
        return true;
    }
}